﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Collections;
using log4net;
using FSS.BLL.Entities;
namespace FSS.BLL.Services
{
    #region VoucherDetailService

    /// <summary>
    /// VoucherDetailService object for Ibatis mapped table 'VoucherDetail'.
    /// </summary>
    public class VoucherDetailService : IVoucherDetailService
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(VoucherDetailService).Name);
        private static volatile VoucherDetailService _VoucherDetailService = null;
        public static VoucherDetailService GetInstance()
        {
            if (_VoucherDetailService == null)
            {
                lock (typeof(VoucherDetailService))
                {
                    if (_VoucherDetailService == null) // double-check
                        _VoucherDetailService = new VoucherDetailService();
                }
            }
            return _VoucherDetailService;
        }
        public VoucherDetailService()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        /// <summary>
        /// Tự động thêm và cập nhật chi tiết
        /// </summary>
        /// <param name="objE"></param>
        /// <returns></returns>
        public int InsertUpdate(VoucherDetailEntity objE)
        {
            try
            {
                ExecuteInsert("VoucherDetailInsertUpdate", objE);
                return 1;
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
                return 0;
            }
        }
        /// <summary>
        /// Delete Voucher Detail by Voucher ID
        /// </summary>
        /// <param name="voucherId"></param>
        /// <returns></returns>
        public bool DeleteVoucherDetailByVoucherId(long voucherId)
        {
            try
            {
                VoucherDetailEntity objE = new VoucherDetailEntity();
                objE.VoucherId = voucherId;
                ExecuteDelete("VoucherDetailDeleteByVoucherId", objE);
                return true;
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
                return true;
            }
        }
        
        /// <summary>
        /// Thêm chi tiết phiếu chuyển kho
        /// </summary>
        /// <param name="objE"></param>
        /// <returns></returns>
        public int DeleteVoucherTransferStock(long voucherId, int productId)
        {
            try
            {
                VoucherDetailEntity objE = new VoucherDetailEntity();
                objE.VoucherId = voucherId;
                objE.ProductId = productId;
                ExecuteDelete("VoucherDetailDeleteTransferStock", objE);
                return 1;
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
                return 0;
            }
        }
        
        public IList<VoucherDetailEntity> GetAllDetailByMasterID(long _voucherId)
        {
            try
            {
                VoucherDetailEntity _voucherDetailE = new VoucherDetailEntity();
                _voucherDetailE.VoucherId = _voucherId;
                return ExecuteQueryForList<VoucherDetailEntity>("VoucherDetailLoadAllByMasterID", _voucherDetailE);
            }
            catch (Exception ex)
            {
                throw ex;
                //log.Debug(ex.Message);
                //return null;
            }
        }
        public IList<VoucherDetailEntity> GetAllDetailTranferStockByMasterID(long _voucherId)
        {
            try
            {
                VoucherDetailEntity _voucherDetailE = new VoucherDetailEntity();
                _voucherDetailE.VoucherId = _voucherId;
                return ExecuteQueryForList<VoucherDetailEntity>("VoucherDetailLoadTranferStockByMasterID", _voucherDetailE);
            }
            catch (Exception ex)
            {
                throw ex;
                //log.Debug(ex.Message);
                //return null;
            }
        }
        
        public bool CheckExistProduct(VoucherDetailEntity _voucherDetailE)
        {
            try
            {
                ExecuteQueryForObject("VoucherDetailCheckExist", _voucherDetailE);
                if (_voucherDetailE.TotalRow > 0)
                    return true;
                return false;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                return false;
            }
        }
        public IList<VoucherDetailEntity> WareHouseCheckStockLoad(VoucherDetailEntity _voucherDetailE)
        {
            try
            {
                return ExecuteQueryForList<VoucherDetailEntity>("WareHouseCheckStockLoad", _voucherDetailE);
            }
            catch (Exception ex)
            {
                //throw ex;
                log.Debug(ex.Message);
               return null;
            }
        }
        

    }
    #endregion
}
